package com.kele.system.config;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Description:
 * @Author: Kele
 * @CreateDate: 2023/4/25 21:34
 * @Version:
 */
@Aspect
@Configuration
public class LogAspect {

    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut(value = "execution(public * com.kele.system.controller.*.* (*))")
    public void logAspect() {
    }

    @Around(value = "logAspect()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
        String requestId = UUID.randomUUID().toString().replace("-", "");
        //打印入参
        log.info("requestId={},请求入参:{}", requestId, JSON.toJSONString(joinPoint.getArgs()));

        Object object = joinPoint.proceed();
        //打印出参
        log.info("requestId={},请求出参:{}", requestId, JSON.toJSONString(object));
        return object;
    }
}
